package gestion_bd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConectorBD {
	private static ConectorBD conectorSingleton= null;
	
	private static final String NBRE_BD = "voto_electronico";
	private static final String URL = "jdbc:mysql://localhost:3306/"+NBRE_BD;
	private Connection conexion;
	private Statement st;
	
	private ConectorBD(){
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			st=null;
			
		} catch (Exception e) {
			System.out.printf(e.getMessage());
		}
	}
	public static ConectorBD getInstancia(){
		if(conectorSingleton == null)
			conectorSingleton = new ConectorBD();
		return conectorSingleton;
	}
	
	public void conectar(String usr, String pass)throws SQLException{
		
			conexion = DriverManager.getConnection(URL, usr, pass);
	}
	
	public ResultSet consulta(String sentencia)throws SQLException{
		st = conexion.createStatement();
		boolean esConsulta = st.execute(sentencia);
		ResultSet resultado=null;
		if(esConsulta){
			resultado= st.getResultSet();
		}
		
		return resultado;
	}
	
	public int modificarBD(String sentencia)throws SQLException{
		st = conexion.createStatement();
		int cantModificaciones = st.executeUpdate(sentencia);
		return cantModificaciones;
	}
	
	public int cantModificaciones()throws SQLException{
		return st.getUpdateCount();
	}
	public void desconectar()throws SQLException{
		conexion.close();
	}
	public void cerrarConsulta()throws SQLException{
		st.close();
	}
	

}